#include "TimeElapsedTimer.h"

// Constructor
CTimeElapsedTimer::CTimeElapsedTimer(void)
: hours			( 0 )
, minutes		( 0 )
, seconds		( 0 )
, milliseconds	( 0 )
, old_time		( 0 )
, run_timer		( false )
{
	// Reset Timer
	this->StopTimer();
}

// Destructor
CTimeElapsedTimer::~CTimeElapsedTimer(void)
{
}

// Get Elapsed Time
void CTimeElapsedTimer::GetElapsedTime(long& hours, long& minutes, long& seconds, long& milliseconds, bool return_milliseconds)
{
	// Return In H:M:S:MS Format
	hours			= this->hours;
	minutes			= this->minutes;
	seconds			= this->seconds;
	milliseconds	= this->milliseconds;

	// If Chosen To Return In Milliseconds Instead Of H:M:S:MS Format
	if ( return_milliseconds ) ConvertTimeToMilliseconds( hours, minutes, seconds, milliseconds );
}

// Get Elapsed Time In Milliseconds
long CTimeElapsedTimer::GetElapsedTime(void)
{
	return GetTimeInMilliseconds( hours, minutes, seconds, milliseconds );
}

// Check If Timer Is Running
bool CTimeElapsedTimer::CheckIfTimerRunning(void) const
{
	return this->run_timer;
}

// Update Timer
void CTimeElapsedTimer::UpdateTimer(void)
{
	// If Timer Is Running
	if ( run_timer )
	{
		// Get Interval Between Each Update
		long interval = timeGetTime() - old_time;

		// Update Elapsed Time
		if ( interval > 0 )
		{
			// Update Timer
			this->milliseconds += interval;

			// Convert Time To H:M:S:MS Format
			ConvertTimeFromMilliseconds( hours, minutes, seconds, milliseconds );

			// Set Old Time To Current Time
			this->old_time = timeGetTime();
		}
	}
}

// Start Timer
void CTimeElapsedTimer::StartTimer(const long hours, const long minutes, const long seconds, const long milliseconds)
{
	// Set Time
	this->hours			= hours;
	this->minutes		= minutes;
	this->seconds		= seconds;
	this->milliseconds	= milliseconds;

	// Initialise And Start Timer
	this->old_time		= timeGetTime();
	this->run_timer		= true;
}

// Pause Timer
void CTimeElapsedTimer::PauseTimer(const bool pause)
{
	// Pause Timer
	if ( pause )
	{
		this->old_time	= 0;
		this->run_timer	= false;
	}
	// Unpause Timer
	else this->StartTimer( hours, minutes, seconds, milliseconds );
}

// Stop/Reset Timer
void CTimeElapsedTimer::StopTimer(void)
{
	// Reset Time
	this->hours			= 0;
	this->minutes		= 0;
	this->seconds		= 0;
	this->milliseconds	= 0;

	// Reset Timer
	this->old_time		= 0;
	this->run_timer		= false;
}